<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sdksystem extends CI_Controller {

    public function index(){
    
          $this->_header(0);              
    }
    
    public function SiteManage(){
          
          $this->_header(1);  
    }
    
    public function usermanage(){
          $this->_header(2);
    }
    
    public function sitelist(){
			$this->load->model('website_model','wm');		

			$this->load->library('myclass');
			
			$curr_page = $this->uri->segment(3)?$this->uri->segment(3):1;
			
			$total_page = $this->wm->websitenumrows();
			
			$size = 15;
			
			$siteinfo = $this->wm->searchtable('sdk_site',$curr_page-1,$size);
	
			$pagination = $this->myclass->pagination($curr_page,$total_page/$size,'/sdksystem/sitelist');			

           	$this->_header(1, 'sitelist', $siteinfo,$pagination);
    }
    
    public function addsite(){
        $this->load->library('myclass');

        $sql = $this->db->query("select * from sdk_user");
        
        $data['randomkey'] = $this->myclass->randomkeys(8);
        $data['q'] = $sql->result();
        
        $this->_header(1, 'addsite', $data);
    }
    
    public function cardholdermanage(){
    	
    	$this->load->model('member_model', 'mm');
    	
    	$this->load->library('myclass');
    		
    	$curr_page = $this->uri->segment(3)?$this->uri->segment(3):1;
    		
    	$total_page = $this->mm->cardholdernumrows();
    		
    	$size = 15;
    		
    	$siteinfo = $this->mm->member_list($curr_page-1,$size);
    	
    	$pagination = $this->myclass->pagination($curr_page,$total_page/$size,'/sdksystem/cardholdermanage');
    	
    	$this->_header(4, 'memberlist', $siteinfo,$pagination);
    }
	
	public function question(){
		
		$this->load->model('question_model', 'qm');
		 
		$this->load->library('myclass');
		
		$curr_page = $this->uri->segment(3)?$this->uri->segment(3):1;
		
		$total_page = $this->qm->questionnumrows();
		
		$size = 15;
		
		$siteinfo = $this->qm->question_list($curr_page-1,$size);
		 
		$pagination = $this->myclass->pagination($curr_page,$total_page/$size,'/sdksystem/question');
		
		$this->_header(5, 'questionlist', $siteinfo,$pagination);

	}
	
	public function answer(){
		$this->load->model('question_model', 'qm');
		$qid = $this->uri->segment(3);		
		$questions['que'] = $this->qm->get_one_question($qid);
		$questions['allque'] = $this->qm->get_questioninfo($qid);

		$this->load->model('order_model', 'om');
		$uid = $questions['que'][0]->uid;
		$questions['order'] = $this->om->get_orderids($uid);

		$this->_header(5, 'answer', $questions);
	}
    
    public function userlist(){
      $this->_header(2);
    }
    
    public function adduser(){
      $this->_header(2);
    }
    
    public function shippmanage(){
       $this->_header(3);
    }
	
	public function emailtemplate(){
		
		$this->load->model('email_model','em');
		$a = array(
				'regester' => array(
						'tempinfo' => '',
						'temptitle' => ''
				),
				'notice' => array(
						'tempinfo' => '',
						'temptitle' => ''
				),
				'forgetpwd' => array(
						'tempinfo' => '',
						'temptitle' => ''
				),
				'fnotice' => array(
						'tempinfo' => '',
						'temptitle' => ''
				),
				'addorder' => array(
						'tempinfo' => '',
						'temptitle' => ''
				),
				'changestatus' => array(
						'tempinfo' => '',
						'temptitle' => ''
				),
				'question' => array(
						'tempinfo' => '',
						'temptitle' => ''
				)
		);
		$templist = $this->em->temp_e_list();
		foreach ($templist as $k=>$v){
			if($v->tempname == 'regester'){
				$a['regester']['tempinfo'] = $v->tempinfo;
				$a['regester']['temptitle'] = $v->temptitle;
			} 
			if($v->tempname == 'notice'){
				$a['notice']['tempinfo'] = $v->tempinfo;
				$a['notice']['temptitle'] = $v->temptitle;
			} 
			if($v->tempname == 'forgetpwd'){
				$a['forgetpwd']['tempinfo'] = $v->tempinfo;
				$a['forgetpwd']['temptitle'] = $v->temptitle;
			}
			if($v->tempname == 'fnotice'){
				$a['fnotice']['tempinfo'] = $v->tempinfo;
				$a['fnotice']['temptitle'] = $v->temptitle;
			}
			if($v->tempname == 'addorder'){
				$a['addorder']['tempinfo'] = $v->tempinfo;
				$a['addorder']['temptitle'] = $v->temptitle;
			}
			if($v->tempname == 'changestatus'){
				$a['changestatus']['tempinfo'] = $v->tempinfo;
				$a['changestatus']['temptitle'] = $v->temptitle;
			}
			if($v->tempname == 'question'){
				$a['question']['tempinfo'] = $v->tempinfo;
				$a['question']['temptitle'] = $v->temptitle;
			}
		}

		$this->_header(6,'emailtemplate',$a);
	}
    
    public function shipplist(){
        $q = $this->db->query("SELECT * FROM sdk_shipp");
       
    
        $this->_header(3, 'shipplist', $q->result());
    }
    
    public function  addshipp(){
         $this->_header(3, 'addshipp');
    }
	
	public function order(){
		
		$this->load->model('order_model', 'om');
		$this->load->model('member_model', 'mm');
		
		$this->load->library('myclass');
			
		$curr_page = $this->uri->segment(3)?$this->uri->segment(3):'';
		
		$status = isset($_POST['status'])?$_POST['status']:'';
		$start_time = isset($_POST['start_time'])?$_POST['start_time']:'';
		$end_time = isset($_POST['end_time'])?$_POST['end_time']:'';
		$ip = isset($_POST['ip'])?$_POST['ip']:'';
		$sitename = isset($_POST['sitename'])?$_POST['sitename']:'';
		$email = isset($_POST['email'])?$_POST['email']:'';
		
		$where = '';
		$cxtj = array();
		
		if($status||$start_time||$end_time||$ip||$sitename||$email){
			
			if($status=='all') $status='';
			if($status){
				$cxtj[] = " o.status='$status' ";
			}			
			if($start_time){
				$start_time = strtotime($start_time);
				$cxtj[] = " o.create_time>'$start_time' ";
			}
			if($end_time){
				$end_time = strtotime($end_time)+24*60*60;
				$cxtj[] = " o.create_time<'$end_time' ";
			}
			if($ip){
				$data = " ip='$ip' ";
				$usinfo = $this->mm->get_member_info($data);
				$cxtj[] = " o.uid='$usinfo->mid' ";
			}
			if($sitename){
				$this->load->model('website_model', 'wm');
				$data = " sitename='$sitename' ";
				$siteinfo = $this->wm->get_site_info($data);
				$cxtj[] = " o.siteid='$siteinfo->siteid' ";
			}
			if($email){
				$data = " email='$email' ";
				$usinfo = $this->mm->get_member_info($data);
				$cxtj[] = " o.uid='$usinfo->mid' ";
			}
			
			$where = implode(" and ",$cxtj);
			$this->session->set_userdata('orderfind',$where);
		}else{
			$allsession = $this->session->all_userdata();
			$where = isset($allsession['orderfind'])?$allsession['orderfind']:'';			
		}
		
		if($where) $where = ' where '.$where;
		
		if($curr_page=='') $curr_page=1;
			
		$total_page = $this->om->ordernumrows(str_replace('o.','',$where));
			
		$size = 15;
			
		$siteinfo = $this->om->get_order_list($curr_page-1,$size,$where);
		
		$pagination = $this->myclass->pagination($curr_page,$total_page/$size,'/sdksystem/order');
	
		$this->_header(7, 'orderlist', $siteinfo,$pagination);
	}
	
	public function delorder(){
	
		$this->load->model('order_model', 'om');
	
		$this->om->delorder($this->uri->segment(3));
		
		echo "<script>location.href='".$_SERVER['HTTP_REFERER']."'</script>";

	}
	
	public function updatestatus(){
	
		$this->load->model('order_model', 'om');
		if($this->om->updatestatus($this->input->post('orderid'),$this->input->post('status'))){
			echo "<script>alert('update successful');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
		}else{
			echo "<script>alert('update successful');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
		}
	}
	
	public function updateshipstatus(){
		$this->load->model('order_model', 'om');
		if($this->om->updateshipstatus($this->input->post('orderid'),$this->input->post('shipstatus'),$this->input->post('shipno'))){
			echo "<script>alert('update successful');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
		}else{
			echo "<script>alert('update successful');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
		}
	}
	
	public function updatechengben(){
		$this->load->model('order_model', 'om');
		if($this->om->updatechengben($this->input->post('orderid'),$this->input->post('chengben'))){
			echo "<script>alert('update successful');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
		}else{
			echo "<script>alert('update successful');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
		}
	}
	
	public function updatebeizhu(){
		$this->load->model('order_model', 'om');
		if($this->om->updatebeizhu($this->input->post('orderid'),$this->input->post('beizhu'))){
			echo "<script>alert('update successful');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
		}else{
			echo "<script>alert('update successful');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
		}
	}
	
	public function order_detail(){
		$this->load->model('order_model', 'om');
		
		$re = $this->om->order_detail($this->uri->segment(3));
                //var_dump($re);
		$this->_header(7, 'order_detail', $re);
	}
	
	public function currency(){
		$siteid = $this->uri->segment(3);
		
		$this->load->model('currency_model', 'cm');
		
		$q = $this->cm->currency_by_site($siteid);
		
		$this->_header(1, 'currency', $q);
	}
	
	public function spreadlist(){
		
		$this->load->model('spreaduser_model', 'sm');
		
		$this->load->library('myclass');
			
		$curr_page = $this->uri->segment(3)?$this->uri->segment(3):1;
			
		$total_page = $this->sm->spreadusernumrows();
			
		$size = 15;
			
		$siteinfo = $this->sm->spreaduser_list($curr_page-1,$size);
		
		$pagination = $this->myclass->pagination($curr_page,$total_page/$size,'/sdksystem/spreadlist');
		
		$this->_header(8, 'spreadlist', $siteinfo,$pagination);
	}
	
	public function spreadedit(){
		$this->load->model('spreaduser_model', 'sm');
		
		$mid = $this->uri->segment(3);		
		$a = $this->sm->spread_detail($mid);
		
		foreach ($a['sdk_site'] as $k=>$v){
			$a['sdk_site'][$k]->percentage = $this->sm->site_percentage($mid,$v->siteid);
		}
		
		$a['sites'] = $this->sm->get_sp_site();
		
		$this->_header(8, 'spreadedit', $a);
	}
	
	public function withdrawal(){
		
		$mid = $this->input->get('mid')?$this->input->get('mid'):'';
		
		$getname = $mid?'mid':'';
			
		$getnum = $mid?$mid:'';
		
		$this->load->model('spreaduser_model', 'sm');
		
		$this->load->library('myclass');
			
		$curr_page = $this->uri->segment(3)?$this->uri->segment(3):1;
			
		$total_page = $this->sm->withdrawalnumrows();
			
		$size = 15;
			
		$siteinfo = $this->sm->withdrawal_list($curr_page-1,$size,$mid);
		
		$pagination = $this->myclass->pagination($curr_page,$total_page/$size,'/sdksystem/withdrawal',$getname,$getnum);
		
		foreach ($siteinfo as $k=>$v){
				
			$q = $this->db->get_where('sdk_spread_user',array('mid'=>$v->mid));
			$re=$q->row();
			$siteinfo[$k]->isactive = $re->isactive;
			$siteinfo[$k]->email = $re->email;
			$siteinfo[$k]->card = $re->card;
		}
		
		$this->_header(8, 'withdrawal', $siteinfo,$pagination);
	}
	

	public function discount(){
		$discount_price = isset($_POST['discount_price'])?$_POST['discount_price']:'';
		$expire_time = isset($_POST['expire_time'])?$_POST['expire_time']:'';
		$this->load->model('discount_model','dm');
		if($discount_price&&$expire_time){
			$this->load->library('myclass');
			$discount_code = $this->myclass->randomkeys(12);
			$expire_time = strtotime($expire_time);
			$re = $this->dm->insert_discount($discount_code,$discount_price,$expire_time);
			if($re){
				echo "<script>alert('insert successful');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
			}else{
				echo "<script>alert('insert successful');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
			}
		}else{
			$discount = $this->dm->get_discount_list();
			$this->_header(9,'discount',$discount,$pagination);
		}		
	}
	

    private function _header($title, $template='', $tdata='',$pagination=''){

          $this->load->library('parser');
          $user =  $this->session->all_userdata();
          
          if ( empty($user['login_name'])){ 
            header("Location:/user");
            exit;
          }
          
          $data['user'] = $user['login_name'];
          
          switch($title){
            case 0:
              $title='首页';break;
            case 1:
              $title = '网站管理';$data['sitemanage']='active';break;
            case 2:
              $title = '权限管理';$data['usermanage']='active';break;
            case 4:
              $title = '持卡人管理';$data['cardholdermanage']='active';break;
            case 3:
              $title = '货运管理';$data['shippmanage']='active';break;
			case 5:
				$title = '问题管理'; $data['question']='active';break;
			case 6:
				$title = '邮件模板'; $data['emailtemplate']='active';break;
			case 7:
				$title = '订单管理'; $data['order']= 'active';break;
			case 8:
				$title = '推广管理';$data['spreadmanage']='active';break;
			case 9:
				$title = '优惠券管理';$data['discount']='active';break;
          }
          
          $data['title'] = $title;

          if (!empty($tdata)) $data['dt'] = $tdata;
          
          if (!empty($pagination)) $data['page'] = $pagination;
          
          $this->parser->parse('header', $data);
          $this->parser->parse('system_header', $data);

          if (!empty($template)) $this->parser->parse($template, $data);
          
          $this->load->view('footer');
    }
}